﻿using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Platform
{
    public class GetPosIntegralRuleDto : EntityDto
    {
        public DateTime? UpdateTime { get; set; }
        public int? Page { get; set; }
    }

    public interface IPosIntegralRuleService : IApplicationService
    {
        Task<PosIntegralRule[]> AttachWarehouse(PosIntegralRule[] data);
    }

    public class PosIntegralRuleService : MyApplicationService, IPosIntegralRuleService
    {
        private readonly IRepository<PosIntegralRule> _posIntegralRule;

        public PosIntegralRuleService(IRepository<PosIntegralRule> posIntegralRule)
        {
            this._posIntegralRule = posIntegralRule;
        }

        public async Task<PosIntegralRule[]> AttachWarehouse(PosIntegralRule[] data)
        {
            var db = await this._posIntegralRule.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PosIntegralRule>(
                data, idGetter: x => x.warehouseID, action: (m, arr) =>
                {
                    m.Warehouse = arr.FirstOrDefault();
                });

            return data;
        }
    }
}
